<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Admin\TbuyProductGroup;
use App\Models\Admin\VbuyProduct;
use App\Models\Admin\TvipDirect;
use App\Models\Admin\TvipDirectItem;
use App\Models\Admin\TbuyProductClass;
use App\Models\Admin\AdminAction;


class GlobalExchangeController extends CommonController {

	public function __construct()
	{
		parent::__construct();
		$this->checkLogin();
	}
	
	/******************直销套餐部分******************/
	/**
	 * 直销套餐部分
	 */
	public function direct(){
		//获取直销套餐信息
		$data = DB::table('tvip_direct')->orderBy('id','desc')->get();
		return view('admin.globalexchange.direct',['data'=>$data]);
	}
	
	/**
	 * 添加直销套餐页面展示
	 **/
	public function direct_add(){
		//获取直销套餐ID号
		$id = request('id');

		//判断ID号是否存在
		if(!empty($id)) {
			//查询直销套餐信息（根据ID号）
			$data = DB::table('tvip_direct')->where(['id'=>$id])->first();
			return view('admin.globalexchange.direct_add', ['data'=>$data]);
		}
		return view('admin.globalexchange.direct_add');
	}
	
	/**
	 * 添加直销套餐
	 **/
	public function direct_add_submit(Request $request){
		//获取表单数据
		$data = $request->only(['id','name','image','amount','amount_cost','pv']);
		//定义校对规则
		$validate_rules = [
				'name'			=> 'required',
				'amount'		=> 'required',
				'amount_cost'	=> 'required',
				'pv'			=> 'required',
			];
		//如果ID号不存在，则添加操作
		if(!$data['id']) {
			$validate_rules['image'] = 'required';
		}
		//如果image属性为null
		if(is_null($data['image'])) {
			unset($data['image']);
		}
		// 必填验证
		$this->validate($request,$validate_rules);

		//如果直销ID号不存在（为0就是不存在）
		if(!$data['id']) {
			//添加操作
			$data['id'] = TvipDirect::getNextSeq();
			TvipDirect::insert($data);
			//记录用户行为日志
			record_action(AdminAction::getNextSeq(),'GlobalExchange','direct_add_submit','添加直销套餐',json_encode($_POST));
			
		}else {
			//修改操作
			TvipDirect::where(['id'=>$data['id']])->update($data);
			record_action(AdminAction::getNextSeq(),'GlobalExchange','direct_add_submit','编辑直销套餐',json_encode($_POST));
		}
		return $this->ajax_success('OK',$data);
	}
	
	/**
	 * 删除直销套餐
	 **/
	public function direct_del(Request $request){
		//获取直销套餐ID号，根据ID号，删除对应的记录
		$result = TvipDirect::where(['id'=>$request->id])->delete();

		//如果删除成功
		if($result) {
			//查询该直销套餐是否存在子套餐
			$data = DB::table('tvip_direct_item')->where(['direct_id'=>$request->id])->first();
			if($data) {
				//删除子套餐（删除子套餐）
				DB::table('tvip_direct_item')->where(['direct_id'=>$request->id])->delete();
			}
			record_action(AdminAction::getNextSeq(),'GlobalExchange','direct_del','删除直销套餐',json_encode($_POST));
		}
		$this->ajax_return($result);
	}
	
	// 直销套餐子商品部分
	public function direct_item(Request $request){
		//获取父分类ID号（直销套餐ID号）
		$direct_id = $request->direct_id;
		//查询子分类（直销子套餐）
		$data = DB::table('tvip_direct_item')->where(['direct_id'=>$direct_id])->orderBy('id', 'desc')->get();

		return view('admin.globalexchange.direct_item', ['data'=>$data, 'direct_id'=>$direct_id]);
	}
	
	
	public function direct_item_add(){
		//获取直销子套餐ID号
		$id = request('id');
		$direct_id = request('direct_id');

		//判断ID号是否存在
		if(!empty($id)) {
			//查询直销子套餐信息（根据ID号）
			$data = DB::table('tvip_direct_item')->where(['id'=>$id])->first();
			return view('admin.globalexchange.direct_item_add', ['data'=>$data, 'direct_id'=>$direct_id]);
		}
		return view('admin.globalexchange.direct_item_add', ['direct_id'=>$direct_id]);
	}
	
	/**
	 * 添加直销套餐的子套餐
	 **/
	public function direct_item_add_submit(Request $request){
		//获取表单数据
		$data = $request->only(['id','direct_id','name','image','total','amount','sku','details']);

		//定义校验规则
		$validate_rules = [
				'name'			=> 'required',
				'total'			=> 'required',
				'amount'		=> 'required',
			];
		//如果ID号不存在，则添加操作
		if(!$data['id']) {
			$validate_rules['image'] = 'required';
		}
		//如果image属性为null
		if(is_null($data['image'])) {
			unset($data['image']);
		}
		// 必填验证
		$this->validate($request,$validate_rules);
		//如果ID号不存在
		if(!$data['id']) {
			//添加操作(获取ID号)
			$data['id'] = TvipDirectItem::getNextSeq();
			TvipDirectItem::insert($data);
			record_action(AdminAction::getNextSeq(),'GlobalExchange','direct_item_add_submit','添加直销套餐的子套餐',json_encode($_POST));
		}else {
			//修改操作
			TvipDirectItem::where(['id'=>$data['id']])->update($data);
			record_action(AdminAction::getNextSeq(),'GlobalExchange','direct_item_add_submit','编辑直销套餐的子套餐',json_encode($_POST));
		}
		return $this->ajax_success('OK',$data);
	}
	
	public function direct_item_del(Request $request){
		//获取直销子套餐ID号
		$id = $request->input()['id'];
		
		//删除直销子套餐
		$result = DB::table('tvip_direct_item')->where(['id'=>$id])->delete();
		record_action(AdminAction::getNextSeq(),'GlobalExchange','direct_item_del','删除直销套餐的子套餐',json_encode($_POST));
		$this->ajax_return($result);
	}

	/******************直销订单部分******************/
	/**
	 * 直销订单列表
	 **/
	public function direct_order() {
		//查询直销套餐订单信息
		$data = DB::table('tvip_direct_order')
				->select('tvip_direct_order.*', 'tvip_direct.name as direct_name', 'tnet_reginfo.nodename','tnet_reginfo.mobileno', 'tbuy_product_group.name as product_group_name')
				->join('tvip_direct', 'tvip_direct_order.direct_id', '=', 'tvip_direct.id')
				->join('tnet_reginfo', 'tvip_direct_order.node_id', '=', 'tnet_reginfo.nodeid')
				->join('tbuy_product_group', 'tvip_direct_order.product_group_id', '=', 'tbuy_product_group.id')
				->where('tvip_direct_order.status', '>', '0')
				->orderBy('tvip_direct_order.id', 'desc')
				->paginate(1);
		//展示模板
		return view('admin.globalexchange.direct_order', ['data'=>$data]);
	}

	/**
	 * 直销订单详情
	 **/
	public function direct_order_detail(Request $request) {
		//获取直销订单ID号
		$id = $request->input()['id'];
		//查询对应直销订单ID号的信息
		$data = DB::table('tvip_direct_order')->where(['id'=>$id])->first();
		//保存参数到data中
		$data->nodename = $request->input()['nodename'];
		$data->direct_name = $request->input()['direct_name'];
		$data->product_group_name = $request->input()['product_group_name'];
		$data->mobileno = $request->input()['mobileno'];

		//解析支付详情的json数据
		$data->pay_detail = json_decode($data->pay_detail, true);

		//展示模板
		return view('admin.globalexchange.direct_order_detail', ['data'=>$data]);
	}

	/**
	 * 修改直销订单的第三方信息
	 **/
	public function direct_order_edit(Request $request) {
		//获取表单数据
		$data = $request->input();
		//修改直销订单数据
		$result = DB::table('tvip_direct_order')->where(['id'=>$data['id']])->update($data);
		record_action(AdminAction::getNextSeq(),'GlobalExchange','direct_order_edit','编辑直销订单的第三方信息',json_encode($_POST));
		return $this->ajax_success('OK',$data);
	}
	
	/******************系统套餐部分******************/
	public function direct_group(){
		$data = DB::table('tbuy_product_group')->orderBy('id','desc')->get();
		return view('admin.globalexchange.direct_group',['data'=>$data]);
	}
	
	public function direct_group_add(){
		$id = request('id');
		$data = DB::table('tbuy_product_group')->where(['id'=>$id])->first();
		return view('admin.globalexchange.direct_group_add',['data'=>$data]);
	}
	
	// 商品搜索
	public function direct_product_search(){
		$keyword = request('keyword');
		if(!$keyword){
			$this->ajax_error('请输入关键字');
		}
		// 排除掉秒杀的商品
		$miaosha = TbuyProductClass::miaosha();
//		DB::listen(function($query){
//			echo $query->sql;
//		});
		$data = VbuyProduct::select('product_id','product_name','image')->groupBy('product_id')->groupBy('product_name')->groupBy('image')->having('product_name','like','%'.$keyword.'%')->havingRaw('product_id not in ('.implode(',',$miaosha).')')->active()->get();
		return $this->ajax_success('OK',$data);
	}
	
	public function direct_group_add_submit(Request $request){
		// 必填验证，前端 post() 方法会截取这个422状态并弹窗提示用户
		$this->validate($request,[
			'name'		=> 'required',
			'product_id'=> 'required',
			'profit'	=> 'required',
			'pv'		=> 'required',
		]);
		$id = $request->id;
		$data = $request->input();
		if($id){
			TbuyProductGroup::where(['id'=>$id])->update($data);
			record_action(AdminAction::getNextSeq(),'GlobalExchange','direct_group_add_submit','编辑系统套餐',json_encode($_POST));
		}else{
			$id = TbuyProductGroup::getNextSeq();
			$add = [
				'id'		=> $id,
				'product_id'=> $data['product_id'],
				'remarks'	=> '',
				'name'		=> $data['name'],
				'pv'		=> $data['pv'],
				'image'		=> $request->image,
				'profit'	=> $request->profit,
			];
			TbuyProductGroup::insert($add);
			record_action(AdminAction::getNextSeq(),'GlobalExchange','direct_group_add_submit','添加系统套餐',json_encode($_POST));
		}
		return $this->ajax_success('OK',$data);
	}
	
	public function direct_group_del(Request $request){
		$id = $request->id;
		$var = TbuyProductGroup::where(['id'=>$id])->delete();
		record_action(AdminAction::getNextSeq(),'GlobalExchange','direct_group_del','删除系统套餐',json_encode($_POST));
		$this->ajax_return($var);
	}
}